package com.college.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college.eduService.client.VodClient;
import com.college.eduService.entity.*;
import com.college.eduService.entity.vo.ChapterVo;
import com.college.eduService.entity.vo.VideoVo;
import com.college.eduService.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college.eduService.mapper.EduChapterMapper;
import com.college.eduService.service.EduChapterService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService{

    @Autowired
    private EduVideoService videoService;//注入小节的service
    @Autowired
    VodClient vodClient;

    //根据课程id查询所有章节以及其小节
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1 根据课程id查询课程里面所有的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        wrapperChapter.orderByAsc("sort");
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
        //2 查询小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        wrapperVideo.orderByAsc("sort");
        List<EduVideo> eduVideoList = videoService.list(wrapperVideo);
        // 存储最终结果
        List<ChapterVo> result = new ArrayList<>();

        for (EduChapter eduChapter:eduChapterList){
            ChapterVo chapterVo = new ChapterVo();
            //将eduChapter的属性赋值给chapterVo（前提是属性一致）
            BeanUtils.copyProperties(eduChapter, chapterVo);
            result.add(chapterVo);

            // 封装小节
            List<VideoVo> children = new ArrayList<>();
            for (EduVideo eduVideo:eduVideoList){
                // 判断其父级 id 是否等于
                if (eduVideo.getChapterId().equals(eduChapter.getId()) == true) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    children.add(videoVo);
                }
            }
            chapterVo.setChildren(children);
        }
        return result;
    }

    //判断其章节下是否有小节，如果没有则删除,如果有则全部删除
    @Override
    @Transactional
    public boolean deleteChapter(String chapterId) {
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        if(count>0){
            List<EduVideo> videos=videoService.list(wrapper);
            for(EduVideo video:videos){
                vodClient.removeAlyVideo(video.getVideoSourceId());
            }
            //删除章节
            baseMapper.deleteById(chapterId);
            //删除小节
            return videoService.remove(wrapper);
        }else{
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    //根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String id) {
        QueryWrapper<EduChapter> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",id);
        baseMapper.delete(wrapper);
    }
}
